package leetcode.other.p977;

import java.util.Arrays;

/**
 * 977. 有序数组的平方
 */
class Solution3 {
    public int[] sortedSquares(int[] nums) {
        int[] res = new int[nums.length];
        int i = 0, j = nums.length - 1, k = j;
        while (i <= j) {
            if (nums[i] * nums[i] <= nums[j] * nums[j]) {
                res[k--] = nums[j] * nums[j];
                j--;
            } else {
                res[k--] = nums[i] * nums[i];
                i++;
            }
        }

        return res;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution3().sortedSquares(new int[]{-4,-1,0,3,10})));
        System.out.println(Arrays.toString(new Solution3().sortedSquares(new int[]{-7,-3,2,3,11})));
    }
}